package cn.edu.scau.cmi.xuxingao.hibernate.client;
import java.util.List;
import java.util.Scanner;



import cn.edu.scau.cmi.xuxingao.hibernate.dao.StudentDAO;
import cn.edu.scau.cmi.xuxingao.hibernate.dao.TeacherDAO;
import cn.edu.scau.cmi.xuxingao.hibernate.domain.Student;
import cn.edu.scau.cmi.xuxingao.hibernate.domain.Teacher;
public class ComprehensionConsoleClient {
	public static void main(String[] args) {


		StudentDAO studentDAO=new StudentDAO();
		TeacherDAO teacherDAO=new TeacherDAO();
		Scanner sc=new Scanner(System.in);
		boolean flag=true;
		while(flag) {
			int ch=0;
			System.out.print("1：添加学生\n2：修改学生\n3.删除学生\n4.添加教师\n5：修改教师\n6：删除教师\n");
			System.out.print("1：添加学生\n2：删除学生\n3：添加教师\n4：删除教师\n5：查看所有学生信息\n6：查看所有老师信息\n7：学生选择指导老师\n8：老师选择被指导的学生\n0：退出程序\n");
			System.out.print("请输入功能前的序号，回车确认：");
			ch=sc.nextInt();
			switch(ch) {
			
			case 1:{
				System.out.print("请输入学生学号和姓名，以空格分隔，回车确认：");
				String name;
				Long id;
				id=sc.nextLong();
				name=sc.next();
				Student stu=new Student();
				stu.setId(id);
				stu.setName(name);
				studentDAO.save(stu);
				System.out.println("添加成功");
			}break;
			case 2:{
				System.out.print("请输入要删除学生的学号，回车确认：");
				Long id;
				id=sc.nextLong();
				Student stu=studentDAO.findById(id);
				//System.out.println(stu.getId()+stu.getName());
				studentDAO.delete(stu);
				System.out.println("删除成功");
			}break;
			case 3:{
				System.out.println("请输入老师ID和姓名，以空格分隔，回车确认：");
				String name;
				Long id;
				id=sc.nextLong();
				name=sc.next();
				Teacher tea=new Teacher();
				tea.setId(id);
				tea.setName(name);
				teacherDAO.save(tea);
				System.out.println("添加成功");
			}break;
			case 4:{
				System.out.print("请输入要删除老师的ID，回车确认：");
				Long id;
				id=sc.nextLong();
				Teacher tea=teacherDAO.findById(id);
				//System.out.println(tea.getId()+tea.getName());
				teacherDAO.delete(tea);
				System.out.println("删除成功");
			}break;
			case 5:{
				List<Student> list=studentDAO.findAll();
				for(Student stu:list) {
					System.out.print(stu.getId()+" "+stu.getName()+" 指导老师：");
					if(stu.getTeacher()==null) {
						System.out.println("无");
					}
					else {
						System.out.println(stu.getTeacher().getId()+" "+stu.getTeacher().getName());
					}
				}
			}break;
			case 6:{
				List<Teacher> list=teacherDAO.findAll();
				boolean have;
				for(Teacher tea:list) {
					have=false;
					System.out.print(tea.getId()+" "+tea.getName()+" 指导学生：");
					List<Student> stuList=studentDAO.findAll();
					for(Student stu:stuList) {
						if(stu.getTeacher()!=null) {
							if(stu.getTeacher().getId()==tea.getId()) {
							System.out.print(stu.getName()+" ");
							have=true;
							}
						}
					}
					if(!have)
						System.out.print("无");
					System.out.println();
				}
			}break;
			case 7:{
				Long stuId,teaId;
				System.out.print("请输入被指导学生的学号，回车确认：");
				stuId=sc.nextLong();
				System.out.print("请输入指导老师的ID，回车确认：");
				teaId=sc.nextLong();
				Student stu=studentDAO.findById(stuId);
				Teacher tea=teacherDAO.findById(teaId);
				stu.setTeacher(tea);
				studentDAO.update(stu);
				System.out.println("修改成功");
			}break;
			case 8:{
				Long stuId,teaId;
				System.out.print("请输入指导老师的ID，回车确认：");
				teaId=sc.nextLong();
				System.out.print("请输入被指导学生的学号，回车确认：");
				stuId=sc.nextLong();
				Student stu=studentDAO.findById(stuId);
				Teacher tea=teacherDAO.findById(teaId);
				stu.setTeacher(tea);
				studentDAO.update(stu);
				System.out.println("修改成功");
			}break;
			case 0:flag=false;break;
			
			default: System.out.print("输入有误，请重新选择：");
			}
			
		}
	}
}